<?php
	require_once( '../system/smarty.inc' );
	
	require_once( '../system/mdao/Class_D_DNPW_AREA.php' );
	require_once( '../system/mdao/Class_M_OTHER.php' );
	
	require_once( '../system/mdao/Class_M_PATTERN.php' );
	
	require_once( '../system/Class_ERROR.php' );
	require_once( '../system/login.inc.php' );
	require_once( '../system/aoki_m.php' );
	
	require_once( '../system/Class_PWD.php' );
	Class_PWD::valid_auth(AOKI_AUTH_DNP_AREA, $login['staff_auth']);
	
	$dosearch = FALSE ;
	$dao      = new Class_D_DNPW_AREA;
	
	$patternd     = Class_M_OTHER::get_pattern_array(PLEASE_SELECT_ARR, 23,TRUE);
	$pattern      = $patternd["pattern"];
	$patternsort  = $patternd["sort"];
	
	$now_pattern_sort = 0;
	$dists   = Class_M_OTHER::get_dist_array();
	//$hands   = Class_M_OTHER::get_hand_array();
	
	if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
		 isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
		 $forms =  $_SESSION['MSEARCH']['AREA_AD'];
	}else {
		$forms = $_GET;
		$_SESSION['MSEARCH']['AREA_AD'] = $forms;
	}
	$wherearr =  array();
	
	/*
	if ( isset( $forms['media_name'] ) &&  ( !empty( $forms['media_name'] ) ) ){
		$wherearr[] = sprintf(" media_name =  '%s' ", $forms['media_name'] );
	}
	*/
	
	if ( isset( $forms['hand_name'] ) &&  ( !empty( $forms['hand_name'] ) ) ){
		$wherearr[] = sprintf(" h.hand_name like  '%s' ",  "%".$forms['hand_name']."%" );
	}
	$dist_para = null;
	if ( isset( $forms['dist_name'] ) &&  ( !empty( $forms['dist_name'] ) ) ){
		$dist_para = $forms['dist_name'];
	}
	$shop_para = null;
	if ( isset( $forms['shop_cd'] ) &&  ( !empty( $forms['shop_cd'] ) ) ){
		$shop_para = $forms['shop_cd'];
	}
	$centernums = array();
	$pattern_para = null;
	if ( isset( $forms['pattern_cd'] ) &&  ( !empty( $forms['pattern_cd'] ) ) ){
		$pattern_para =  $forms['pattern_cd'];
		$wherearr[] = sprintf(" a.pattern_cd =  '%s' ",$forms['pattern_cd'] );
		$now_pattern_sort = $patternsort[$forms['pattern_cd']];
		
	}
	
	if ( isset( $forms['upd_f'] ) &&  ( !empty( $forms['upd_f'] ) ) ){
		$wherearr[] = Class_PWD::get_u_time_where_sql($forms['upd_f'], 1 );
	}
	
	if ( isset( $forms['upd_t'] ) &&  ( !empty( $forms['upd_t'] ) ) ){
		$wherearr[] = Class_PWD::get_u_time_where_sql($forms['upd_t'],2 );
	}
	
	/*
		$centernums["c_area_nums"] = empty($centernums["c_area_nums"]) ? 0 : $centernums["c_area_nums"] ;
		$centernums["c_shop_per"]  = empty($centernums["c_shop_per"] ) ? 0 : $centernums["c_shop_per"] ;
		$centernums["c_shop_nums"] = empty($centernums["c_shop_nums"]) ? 0 : $centernums["c_shop_nums"] ;
	*/
	
	//---CSVダウンロード　処理開始
	
	if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
		
		$_GET["csv"] = "";
		$tmpfilename = "areaad".date("Ymd_His");
		header("Content-Type: application/octet-stream"); 
		header("Content-Disposition: attachment; filename=$tmpfilename.csv");
		
		$dao->setTableNameWeek( $forms['matrix_cd']);
		if ($now_pattern_sort == '3'){
			$data = $dao->searchcsv( $wherearr,$forms["pattern_cd"], $dist_para, $shop_para );
			$centernums =  $dao->getcentercount( $forms["pattern_cd"] );
		}else{
			$data    = $dao->seaarchPATcsv($forms["pattern_cd"],  $shop_para);
			$weekpat  = $dao->seaarchPAT($forms['matrix_cd'], $forms["pattern_cd"]);
		}
		$colums = array();
		$col_heads = array(
			'matrix_cd'        => array( 'マトリックスID',  1),
			'year4'            => array( '年度',            1),
			'week_cd'          => array( '週',              1),
			'flier_date'       => array( '折込日',          1),
			'pattern_cd'       => array( 'パターンコード',          1),
			'pattern_name'     => array( '新聞紙名',          1),
			'pattern_media'    => array( 'パターン媒体',          1),
			'dist_name'        => array( '配布地域名',          1),
			'title_cd'         => array( 'マトリックス版ID',          1),
			'title_name'       => array( 'セールタイトル',          1),
			'isirgyu'          => array( 'MAP',          1),
			'size'             => array( 'サイズ',          1),
			'area_nums'        => array( 'エリア部数',          1),
			'hand_name'        => array( '取扱業者',          1),
			'memo'             => array( '備考',          1),
			'shop_cd'          => array( '【店舗】店舗コード',          1),
			'shop_name'        => array( '【店舗】店舗名',          1),
			'zone_nm'          => array( '【店舗】ゾーンAJA名',          1),
			'area_nm'          => array( '【店舗】エリアAJA名',          1),
			'map_no'           => array( '【店舗】マップNo',          1),
			'year_sale'        => array( '【店舗】年商',          1),
			'shop_per'         => array( '【店舗】按分部数%',          1),
			'shop_nums'        => array( '【店舗】計算部数',          1),
			'ins_shop_cd'      => array( '【販売店】GEOコード',          1),
			'ins_shop_name'    => array( '【販売店】販売店名',          1),
			'area_adnums'      => array( '【販売店】地域広告部数',          1),
			'ss_per'           => array( '按分部数%合計',          1),
			'ss_nums'          => array( '按分部数合計/セールタイトル部数合計"',          1),
			'center_flg'       => array( '都心エリアフラグ',          1),
			'c_shop_per'       => array( '都心エリア(按分部数%合計)',          1),
			'c_shop_nums'      => array( '都心エリア(按分部数合計)',          1),
			'all_per'          => array( 'パターン按分部数%合計',          1),
			'all_num'          => array( 'パターン部数合計',          1),
			'u_time'           => array( '更新日時',        3),
			'c_time'           => array( '登録日時',        3),
			'm_user_name'      => array( '更新者',          1)
		);

		//列名
		foreach ($col_heads as $k=>$v) {
			$colums["name"][]   = $k;
			$colums["head"][]   = $v[0];
			$colums["type"][$k] = $v[1];
		}
		//データ整形開始
		$week = Class_M_OTHER::get_by_matrix_cd($forms['matrix_cd'] );
		$week["flier_date"] =  Class_PWD::int8_to_dateD($week["flier_date"]);
	
		$pattern_media = $aoki_pattern_flags[$now_pattern_sort];
		$pattern_name  = $pattern[ $forms['pattern_cd'] ];
		
		$all_per = 0;
		$all_num = 0;
		
		if ($now_pattern_sort == '2'){
			foreach ($data as $k => $v) {
				$data[$k]["hand_name"]  = $weekpat["hand_name"] ;
				$data[$k]["memo"]       = $weekpat["memo"] ;
				$data[$k]["area_nums"]  = $weekpat["area_nums"] ;
			}
			$all_num = $weekpat["shop_nums"] ;
		}
		if ($now_pattern_sort == '3'){
			
			foreach ($data as $k => $v) {
				$all_per = $all_per + $v["shop_per"];
				$all_num = $all_num + $v["shop_nums"];
				
				$data[$k]["c_shop_per"]  = $centernums["c_shop_per"] ;
				$data[$k]["c_shop_nums"]  = $centernums["c_shop_nums"] ;
			}
		}
		//$titlearr =  Class_M_OTHER::get_titles($forms["matrix_cd"]);
		foreach ($data as $k => $v) {
			//$data[$k]["pattern_name"] =$pattern[ $v["pattern_cd"] ] ;
			
			$data[$k]["pattern_name"]  = $pattern_name ;
			$data[$k]["pattern_media"] = $pattern_media ;
			$data[$k]["dist_name"]     = $dists[ $v["dist_id"] ] ;
			
			if (!empty($v["isirgyu"])) {
				$data[$k]["isirgyu"]    = $aoki_map_sort[ $v["isirgyu"] ] ;
			}else {
				$data[$k]["isirgyu"]    = "" ;
			}
			$data[$k]["matrix_cd"]   = $forms['matrix_cd'];
			$data[$k]["year4"]       =  $week["year4"];
			$data[$k]["week_cd"]     =  $week["week_cd"];
			$data[$k]["flier_date"]  =  $week["flier_date"];
			
			
			$data[$k]["all_per"]  = $all_per ;
			$data[$k]["all_num"]  = $all_num ;
			
			

			$data[$k]["c_time"]        = Class_PWD::dateTime_to_YmdHi($v["c_time"]);
			$data[$k]["u_time"]        = Class_PWD::dateTime_to_YmdHi($v["u_time"]);
		}
		//データ整形完了
		$dao->output_csv($data, $colums);
		
		exit;
	}
	
	//---CSVダウンロード　処理完了
	
	if ( isset( $forms['search'] ) && (  !empty( $forms['search'] ) ) ){
		$dosearch = TRUE;
	}
	
	if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
		$dosearch = TRUE;
		$page['current'] = $forms['page'];
	}else{
		$page['current'] = 1;
	}
	
	$page['item'] = PAGE_MAX;
	
	if ($dosearch) {
	
		$dao->setTableNameWeek( $forms['matrix_cd']);
		$centernums =  $dao->getcentercount( $forms["pattern_cd"] );
		if ($now_pattern_sort == '2'){
			$data = $dao->seaarchPAT($forms['matrix_cd'], $forms['pattern_cd'],  $shop_para);
			$page["cnt"]= count($data);
		}
		
		if ($now_pattern_sort == '3'){
			//$page内容は関数内部更新されます。
			$data = $dao->search( $wherearr, $page, $pattern_para,$dist_para, $shop_para);
			$page["cnt"]= count($data);
			$titlearr =  Class_M_OTHER::get_titles($forms["matrix_cd"]);
			//表示ために整形開始
			foreach ($data as $k => $v ) {
				$data[$k]["pattern_name"] =$pattern[ $v["pattern_cd"] ] ;
				$data[$k]["dist_name"] =$dists[ $v["dist_id"] ] ;
				if (isset($titlearr[ $v["title_cd"] ])) {
					$title =  $titlearr[ $v["title_cd"] ];
					$data[$k]["title_name"] = $title["titlecell"];
					$data[$k]["isirgyu"]    = $aoki_map_sort[ $title["map"] ] ;
					$data[$k]["size"]       = $title["size"] ;
				}else {
					$data[$k]["title_name"] = "" ;
					$data[$k]["isirgyu"]    = "" ;
					$data[$k]["size"]       = "" ;
				}
				//$data[$k]["hand_name"] =$hands[ $v["hand_cd"] ] ;
			}
		}
	}
	//表示ために整形完了
	//==================================周
	if (empty($forms["year4"])) {
		$forms["year4"] = date('Y');
	}
	$smarty->assign('year4',  Class_PWD::get_years());
	
	$week = Class_M_OTHER::get_week_array( $forms["year4"], PLEASE_SELECT_ARR, TRUE );
	
	$smarty->assign('week', $week );
	//==================================周
	
	if( isset($forms) ) $smarty->assign('forms', $forms);
	if( isset($data) )  $smarty->assign('data', $data);
	

	if( isset($centernums) )  $smarty->assign('center', $centernums);
	
	if( isset($page) )  $smarty->assign('page', $page);
	$smarty->assign('pattern', $pattern );
	$smarty->assign('patternsort', $now_pattern_sort );
	

	/*
	$block = Class_M_OTHER::get_block_array( PLEASE_SELECT_ARR );
	$smarty->assign('block', $block );
	
	$local= Class_M_OTHER::get_local_array( PLEASE_SELECT_ARR );
	$smarty->assign('local', $local );
	
	
	$flags[""] = "すべて";
	foreach ($aoki_disp_flags as $k => $v) {
		$flags[$k] = $v;
	}
	$smarty->assign('flags', $flags );
	
	//get_pattern_array($please_select = NULL, $sort = 0)
	
	$notisg5 = TRUE ;
	if   ($login['staff_auth'] == 'G-5') {
		$notisg5 = FALSE ;
	}
	$smarty->assign('notisg5', $notisg5 );
	*/
	$smarty->assign('menu', $smarty->fetch('menu.html') );
	$smarty->assign('logout', $smarty->fetch('logout.html') );
	$smarty->assign('footer', $smarty->fetch('footer.html') );
	$smarty->assign('pageing', $smarty->fetch('page.html') );
	$smarty->display('ddnpwarea_index.html');
?>